/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QuanLyDatHang;

import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author PhuongNga
 */
public class DanhSachDatHang extends javax.swing.JPanel {

    private DefaultTableModel modelDatHang = new DefaultTableModel();
    DatHang dh = new DatHang();
    Vector vLay = new Vector();

    public DanhSachDatHang() {
        initComponents();
        try {
            vLay = DatHang.xemDanhSachDatHang();
            this.taiDanhSachDatHang(vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachDatHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //tải data lên table
    public void taiDanhSachDatHang(Vector vLay) throws SQLException {
//        modelDatHang.setRowCount(0);
        Vector cotDatHang = new Vector();

        cotDatHang.addElement("Mã Đặt hàng");
        cotDatHang.addElement("Tên Khách hàng");
        cotDatHang.addElement("Thời gian giao hàng");
        cotDatHang.addElement("Địa chỉ giao hàng");
        cotDatHang.addElement("Tên thuốc");
        cotDatHang.addElement("Số lượng");
        cotDatHang.addElement("Giá");

        modelDatHang.setColumnIdentifiers(cotDatHang);

        DatHang dh1 = new DatHang();
        for (int i = 0; i < vLay.size(); i++) {
            dh1 = (DatHang) vLay.get(i);

            Vector hangDatHang = new Vector();
            hangDatHang.addElement(dh1.getMaDatHang());
            hangDatHang.addElement(dh1.getTenKhachHang());
            hangDatHang.addElement(dh1.getThoiGianGiaoHang());
            hangDatHang.addElement(dh1.getDiaChiGiaoHang());
            hangDatHang.addElement(dh1.getTenThuoc());
            hangDatHang.addElement(dh1.getSoLuong());
            hangDatHang.addElement(dh1.getGia());

            modelDatHang.addRow(hangDatHang);

        }
        tbXemDSDatHang.setModel(modelDatHang);
    }

    //Xóa đặt hàng
    public void xoaDatHang() {
        int r = tbXemDSDatHang.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn mục để xóa", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return;
        }
        int show = JOptionPane.showConfirmDialog(this, "Bạn có đồng ý xóa dữ liệu Đặt hàng ?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            int kt = 0, dem = 0;
            int[] rArr = tbXemDSDatHang.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    int maDatHang = Integer.parseInt(tbXemDSDatHang.getValueAt(rArr[i], 0).toString());
                    kt = DatHang.xoaDatHang(maDatHang);
                    if (kt != 0) {
                        dem++;
                    }
                }
                if (dem > 0) {
                    JOptionPane.showMessageDialog(this, "Xóa " + dem + " Đặt hàng thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                    vLay = DatHang.xemDanhSachDatHang();
                    this.taiDanhSachDatHang(vLay);
                } else {
                    JOptionPane.showMessageDialog(this, "Không xóa được!", "Thông báo", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    //Tìm kiếm Đặt hàng
    public void timKiemDatHang() throws SQLException {
        if (txtTenKhachHang.getText().equals("") && txtTenThuoc.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Bạn phải chọn ít nhất một mục để tìm kiếm");
            return;
        }
        Vector vlist = new Vector();
        if (txtTenKhachHang.getText() != "" && txtTenThuoc.getText().equals("")) {
            vlist.addElement(txtTenKhachHang.getText().trim());
            vLay = DatHang.timKiem("spTimKiemTenKhachHang_DatHang", vlist);
            this.taiDanhSachDatHang(vLay);
        } else if (txtTenKhachHang.getText().equals("") && txtTenThuoc.getText() != "") {
            vlist.addElement(txtTenThuoc.getText().trim());
            vLay = DatHang.timKiem("spTimKiemTenThuoc_DatHang", vlist);
            this.taiDanhSachDatHang(vLay);
        } else {
            vlist.addElement(txtTenKhachHang.getText().trim());
            vlist.addElement(txtTenThuoc.getText().trim());
            vLay = DatHang.timKiem("spTimKiemTenKhachHang_TenThuoc_DatHang", vlist);
            this.taiDanhSachDatHang(vLay);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbXemDSDatHang = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTenThuoc = new javax.swing.JTextField();
        txtTenKhachHang = new javax.swing.JTextField();
        btTimKiem = new javax.swing.JButton();
        btThem = new javax.swing.JButton();
        btTroVe = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh Sách Đặt Hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(102, 255, 0)));

        tbXemDSDatHang.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tbXemDSDatHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbXemDSDatHang);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Tên khách hàng :");

        jLabel2.setText("Tên thuốc :");

        txtTenThuoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTenThuocActionPerformed(evt);
            }
        });

        txtTenKhachHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTenKhachHangActionPerformed(evt);
            }
        });

        btTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Search.png"))); // NOI18N
        btTimKiem.setText("Tìm kiếm");
        btTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimKiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(51, 51, 51)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTenKhachHang, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                            .addComponent(txtTenThuoc)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(btTimKiem)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(txtTenKhachHang))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtTenThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );

        btThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add 1.png"))); // NOI18N
        btThem.setText("Thêm");
        btThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemActionPerformed(evt);
            }
        });

        btTroVe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Go-back.png"))); // NOI18N
        btTroVe.setText("Trở về");
        btTroVe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTroVeActionPerformed(evt);
            }
        });

        btXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Delete.png"))); // NOI18N
        btXoa.setText("Xóa");
        btXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/123.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(111, 111, 111)
                                .addComponent(btTroVe))))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 691, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btThem)
                            .addComponent(btXoa))
                        .addGap(18, 18, 18)
                        .addComponent(btTroVe))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtTenThuocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTenThuocActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTenThuocActionPerformed

    private void txtTenKhachHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTenKhachHangActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTenKhachHangActionPerformed

    private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
        // TODO add your handling code here:
        new ThemDatHang().setVisible(true);
    }//GEN-LAST:event_btThemActionPerformed

    private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
        // TODO add your handling code here:
        this.xoaDatHang();
    }//GEN-LAST:event_btXoaActionPerformed

    private void btTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimKiemActionPerformed
        try {
            // TODO add your handling code here:
            this.timKiemDatHang();
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachDatHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btTimKiemActionPerformed

    private void btTroVeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTroVeActionPerformed
        try {
            // TODO add your handling code here:
            txtTenKhachHang.setText("");
            txtTenThuoc.setText("");
            vLay = DatHang.xemDanhSachDatHang();
            this.taiDanhSachDatHang(vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachDatHang.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btTroVeActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btThem;
    private javax.swing.JButton btTimKiem;
    private javax.swing.JButton btTroVe;
    private javax.swing.JButton btXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbXemDSDatHang;
    private javax.swing.JTextField txtTenKhachHang;
    private javax.swing.JTextField txtTenThuoc;
    // End of variables declaration//GEN-END:variables
}
